package kr.hkit.powerjava.chapter22.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import kr.hkit.powerjava.chapter22.vo.Department;


public class DepartmentManagerImpl implements ItemDaoInterface<Department>{
	private List<Department> arList;
	
	
	public DepartmentManagerImpl(ArrayList<Department> arList) {
		this.arList = arList;
	}

	@Override
	public void addItem(Department item) {
		arList.add(item);
	}

	@Override
	public void delItem(Department item) {
		arList.remove(item);
	}

	@Override
	public void updateItem(Department item) {
		if(searchItem(item)) {
			arList.set(arList.indexOf(item), item);	//인덱스 아이템 찾아서 넣어주기
		} else {
			System.out.println("수정 실패");
		}
	}

	@Override
	public boolean searchItem(Department item) {
		return arList.contains(item);	//Department에서 equals를 오버라이등을 하여야 함.
	}

	@Override
	public void prnItems() {
		prnHeader();
		for(Department d : arList) {
			System.out.println(d);
		}
		System.out.println();
		
	}

	private void prnHeader() {
		String header = String.format("%3s\t%5s\t%3s", "학과번호", "학과명", "학과층");
		System.out.println(header);
	}

	@Override
	public void prnItems(Comparator<Department> comp) {	//정렬하는 부분
		Collections.sort(arList, comp);
		prnItems();
	}

}